package com.googlecode.restitory.gae.hook;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.restitory.gae.Request;
import com.googlecode.restitory.gae.RequestService;
import com.googlecode.restitory.gae.Response;

public class Chain implements RequestService {

	private final List<Hook> hooks = new ArrayList<Hook>();

	public Chain() {

	}

	public Chain(List<Hook> hooks) {
		this.hooks.addAll(hooks);
	}

	public Response execute(Request request) {
		if (hooks.isEmpty()) {
			throw new RuntimeException("chain of hooks finished");
		}
		Hook hook = hooks.remove(0);
		Response ret = hook.execute(request, this);
		return ret;
	}

	public List<Hook> getHooks() {
		return hooks;
	}

	public void addHook(Hook hook) {
		hooks.add(hook);
	}

	public Chain copy() {
		return new Chain(hooks);
	}

}
